<?php
/**
 * 
 *
 * @category   Application_Extensions
 * @package    Recorder
 * @copyright  Copyright 2006-2010 Idragonsolution Developments
 * @license    http://idragonsolution.com/
 * @version    $Id: Bootstrap.php 9747 2012-07-26 02:08:08Z Thien $
 * @author     Thien
 */
class Recorder_IndexController extends Core_Controller_Action_Standard
{
    
     public function init()
    {
        // Check auth
        if( !$this->_helper->requireAuth()->setAuthParams('recorder', null, 'view')->isValid()) {
          return;
        }
    
        // Get viewer info
        $this->view->viewer     = Engine_Api::_()->user()->getViewer();
        $this->view->viewer_id  = Engine_Api::_()->user()->getViewer()->getIdentity();
        
        $id = $this->_getParam('recorder_id', $this->_getParam('id', null));
        if( $id )
        {
          $paint = Engine_Api::_()->getItem('recorder', $id);
          if( $paint )
          {
            Engine_Api::_()->core()->setSubject($paint);
          }
        }
        if( !$this->_helper->requireAuth()->setAuthParams('recorder', null, 'view')->isValid()) return;
    
    }
    
    public function uploadAction()
    {
        $session = new Zend_Session_Namespace('Recorder_Upload_Info');
        // only members can upload recorder
        if( !$this->_helper->requireUser()->checkRequire() ) {
          $session->status = false;
          $session->error = $this->view->translate('Max file size limit exceeded or session expired.');
          return;
        }
    
        // Check method
        if( !$this->getRequest()->isPost() ) {
          $session->status = false;
          $session->error = $this->view->translate('Invalid request method');
          return;
        }
    
        // Check file
        $values = $this->getRequest()->getPost();
        if( empty($values['Filename']) || empty($_FILES['Filedata']) ) {
          $session->status = false;
          $session->error = $this->view->translate('No file');
          return;
        }
    
        $this->view->viewer   = $viewer  = Engine_Api::_()->user()->getViewer();
        
        $session->filename = $values['Filename'];
        // Process
        $table = Engine_Api::_()->getDbtable('recorders', 'recorder');
        $db = $table->getAdapter();
        $db->beginTransaction();
        
        try {
          $file = Engine_Api::_()->getApi('core', 'recorder')->createRecorder($_FILES['Filedata']);
          $recorder = $table->createRow();
          $recorder->owner_id = $viewer->getIdentity();
          $session->status   = true;
          $session->recorder     = $file;
          $recorder->search = 0;
          $recorder->file_id  = $file->getIdentity();
          $recorder->recorder_url = $file->getHref();
          $recorder->save();
          $db->commit();
          
          $session->recorder_id = $recorder->getIdentity();
    
        } catch( Music_Model_Exception $e ) {
          $db->rollback();
    
          $session->status = false;
          $session->error = $this->view->translate($e->getMessage());
          return $this->_helper->json(array('status'=> $session->status,'error'=> $session->error));
          
        }
        
        $session->status = true;
        return $this->_helper->json(array('status'=>true));
    }
    
    public function uploadInfoAction(){
        // only members can upload recorder
        $data = array();
        if( !$this->_helper->requireUser()->checkRequire() ) {
          $data['status'] = false;
          $data['error'] = $this->view->translate('Max file size limit exceeded or session expired.');
          return $this->_helper->json($data);
          
        }
        
        $session = new Zend_Session_Namespace('Recorder_Upload_Info');
        $data['status'] = $session->status;
        $data['error'] = $session->error;
        $data['id']  = $session->recorder_id;
        return $this->_helper->json($data);
    }
    public function indexAction(){
        
    }
    
    public function manageAction()
    {
        // only members can manage recorder
        if( !$this->_helper->requireUser()->isValid() ) {
          return;
        }
        
        $this->view->viewer   = $viewer  = Engine_Api::_()->user()->getViewer();
        
        // Get navigation
        $this->view->navigation = Engine_Api::_()
          ->getApi('menus', 'core')
          ->getNavigation('recorder_main', array(), 'recorder_main_manage');
    
        
        // Get browse params
        $this->view->formFilter = $formFilter = new Recorder_Form_Search();
        $formFilter->removeElement('show');
        if( $formFilter->isValid($this->_getAllParams()) ) {
          $values = $formFilter->getValues();
        } else {
          $values = array();
        }
        $this->view->formValues = array_filter($values);
        
        $values['user_id'] = $viewer->getIdentity();
        // Get paginator
        $values['user'] = Engine_Api::_()->user()->getViewer()->getIdentity();
        $this->view->paginator = $paginator = Engine_Api::_()->recorder()->getPaginator($values);
        $paginator->setItemCountPerPage(Engine_Api::_()->getApi('settings', 'core')->getSetting('recorder.itemperpage', 10));
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
   }
   
   public function browseAction()
    {
        
        if( !$this->_helper->requireUser()->isValid() ) {
          return;
        }
        
        $this->view->viewer   = $viewer  = Engine_Api::_()->user()->getViewer();
        
        // Get navigation
        $this->view->navigation = Engine_Api::_()
          ->getApi('menus', 'core')
          ->getNavigation('recorder_main', array(), 'recorder_main_browse');
    
        
        // Get browse params
        $this->view->formFilter = $formFilter = new Recorder_Form_Search();
        $formFilter->removeElement('show');
        if( $formFilter->isValid($this->_getAllParams()) ) {
          $values = $formFilter->getValues();
        } else {
          $values = array();
        }
        $this->view->formValues = array_filter($values);
        
       
        // Get paginator
        $values['user'] = Engine_Api::_()->user()->getViewer()->getIdentity();
        $this->view->paginator = $paginator = Engine_Api::_()->recorder()->getPaginator($values);
        $paginator->setItemCountPerPage(Engine_Api::_()->getApi('settings', 'core')->getSetting('recorder.itemperpage', 10));
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
       
         $this->_helper->content
            //->setNoRender()
            ->setEnabled();
        
   }
   
    public function viewAction()
    {
          if( !$this->_helper->requireSubject()->isValid() ) return;
    
        $recorder = Engine_Api::_()->core()->getSubject('recorder');
        $viewer = Engine_Api::_()->user()->getViewer();
    
        // if this is sending a message id, the user is being directed from a coversation
        // check if member is part of the conversation
        $message_id = $this->getRequest()->getParam('message');
        $message_view = false;
        if( $message_id ) {
          $conversation = Engine_Api::_()->getItem('messages_conversation', $message_id);
          if( $conversation->hasRecipient(Engine_Api::_()->user()->getViewer()) ) {
            $message_view = true;
          }
        }
        $this->view->message_view = $message_view;
        if( !$message_view && 
            !$this->_helper->requireAuth()->setAuthParams($recorder, null, 'view')->isValid() ) {
          return;
        }
    
        $this->view->recorderTags = $recorder->tags()->getTagMaps();
    
        // Check if edit/delete is allowed
        $this->view->can_edit = $can_edit = $this->_helper->requireAuth()->setAuthParams($recorder, null, 'edit')->checkRequire();
        $this->view->can_delete = $can_delete = $this->_helper->requireAuth()->setAuthParams($recorder, null, 'delete')->checkRequire();
    
        $this->view->viewer_id = $viewer->getIdentity();
        $this->view->rating_count = Engine_Api::_()->paint()->ratingCount($recorder->getIdentity());
        $this->view->recorder = $recorder;
        $this->view->rated = Engine_Api::_()->paint()->checkRated($recorder->getIdentity(), $viewer->getIdentity());
        
        if( $recorder->category_id ) {
          $this->view->category = Engine_Api::_()->paint()->getCategory($recorder->category_id);
        }
        
        $recorder->view_count++;
        $recorder->save();
        
        // Render
        $this->_helper->content
            //->setNoRender()
            ->setEnabled()
            ;
    }
    
    public function deleteAction()
    {
        $viewer = Engine_Api::_()->user()->getViewer();
        $recorder = Engine_Api::_()->getItem('recorder', $this->getRequest()->getParam('recorder_id'));
        if( !$this->_helper->requireAuth()->setAuthParams($recorder, null, 'delete')->isValid()) return;
    
        // In smoothbox
        $this->_helper->layout->setLayout('default-simple');
        
        $this->view->form = $form = new Recorder_Form_Delete();
    
        if( !$recorder )
        {
          $this->view->status = false;
          $this->view->error = Zend_Registry::get('Zend_Translate')->_("Recorder doesn't exists or not authorized to delete");
          return;
        }
    
        if( !$this->getRequest()->isPost() )
        {
          $this->view->status = false;
          $this->view->error = Zend_Registry::get('Zend_Translate')->_('Invalid request method');
          return;
        }
    
        $db = $recorder->getTable()->getAdapter();
        $db->beginTransaction();
    
        try
        {
          Engine_Api::_()->getApi('core', 'recorder')->deleteRecorder($recorder);
          $db->commit();
        }
    
        catch( Exception $e )
        {
          $db->rollBack();
          throw $e;
        }
    
        $this->view->status = true;
        $this->view->message = Zend_Registry::get('Zend_Translate')->_('Recorder has been deleted.');
        return $this->_forward('success' ,'utility', 'core', array(
          'parentRedirect' => Zend_Controller_Front::getInstance()->getRouter()->assemble(array('action' => 'manage'), 'recorder_general', true),
          'messages' => Array($this->view->message)
        ));
    }
  
}